package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.*;
import com.dgut.mapper.IntellectualPropertyMapper;
import com.dgut.mapper.OrderFlagMapper;
import com.dgut.pojo.Contract;
import com.dgut.pojo.IntellectualProperty;
import com.dgut.pojo.OrderFlag;
import com.dgut.service.IntellectualPropertyService;
import com.dgut.service.NoticeService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class IntellectualPropertyServiceImpl implements IntellectualPropertyService {
    @Resource
    private IntellectualPropertyMapper intellectualPropertyMapper;

    @Resource
    private OrderFlagMapper orderFlagMapper;
    @Resource
    private NoticeService noticeService;
    /**
     * start-zwb
     */
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */
    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    //查询所有的知识产权
    public List<IntellectualProperty> getIntellectualPropertyList(){
        return  intellectualPropertyMapper.selectList(null);
    }

    //根据名字模糊查询知识产权
    public List<IntellectualProperty> getIntellectualPropertyByName(String name){
        List<IntellectualProperty> lists = null;
        if(name==null){
            lists = intellectualPropertyMapper.selectList(null);
        }
        else{
            QueryWrapper<IntellectualProperty> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("name",name);
            lists = intellectualPropertyMapper.selectList(queryWrapper);
        }
        return lists;
    }
    //根据id点击详情进入到详情页面
    public IntellectualProperty getIntellectualPropertyDetail(int id){
        IntellectualProperty intellectualProperty = intellectualPropertyMapper.selectById(id);
        return intellectualProperty;
    }

    //分页查询所有的知识产权
    public Object getIntellectualPropertyByPage(PageRequestInfo pageRequestInfo){
        Page<IntellectualProperty> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<IntellectualProperty> resultPage = intellectualPropertyMapper.selectPage(page,null);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }

    //采买方购买知识产权后增加order订单
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.BUYER)
    public Object BuyIntellectualProperty(Integer buyer_id,IntellectualProperty intellectualProperty){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
                OrderFlag orderFlag =new OrderFlag(buyer_id,null, OrderType.INTELLECTUAL_PROPERTY,intellectualProperty.getId(),new Date(),new Date(),
                        new Date(),intellectualProperty.getMoney(), StateForOrder.WAITING_FOR_CONFIRMATION,OnOrOff.ON, ExceptionIdentificationForOrder.NORMAL);
                orderFlagMapper.insert(orderFlag);
                noticeService.orderChangedStateNotice(orderFlag);
                resultMap.put("message","购买成功！");

        return resultMap;
    }

    //平台管理员产品管理
    //分页查询所有的知识产权产品
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object getIntellectualPropertyByPage02(PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();

                Page<IntellectualProperty> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
                Page<IntellectualProperty> resultPage = intellectualPropertyMapper.selectPage(page,null);
                PageResult result = PageUtils.getPageResult(resultPage);
                resultMap.put("result",result);
                return resultMap;
    }


    //增加知识产权商品
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object InsertIntellectualProperty(IntellectualProperty intellectualProperty){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
                intellectualPropertyMapper.insert(intellectualProperty);
                resultMap.put("message","添加成功！");
        return resultMap;
    }

    //上架知识产权商品
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object StackIntellectualProperty(Integer id){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
                intellectualPropertyMapper.StackIntellectualProperty(id,OnOrOff.ON);
                resultMap.put("message","上架成功！");
        return resultMap;
    }

    //下架知识产权商品
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object UndercarriageIntellectualProperty(Integer id){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
                intellectualPropertyMapper.UndercarriageIntellectualProperty(id,OnOrOff.OFF);
                resultMap.put("message","下架成功！");
        return resultMap;
    }

    //编辑更新知识产权商品
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object UpdateIntellectualProperty(IntellectualProperty intellectualProperty){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
                int update = intellectualPropertyMapper.updateById(intellectualProperty);
                if (update>=1){
                    resultMap.put("message","更新成功！");
                    resultMap.put("result",true);
                }
                else {
                    resultMap.put("result",false);
                }
        return resultMap;
    }

    //删除知识产权商品
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object DeleteIntellectualProperty(Integer id){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
                intellectualPropertyMapper.deleteById(id);
                resultMap.put("message","删除成功！");
        return resultMap;
    }
    //前台分页查看已上架的知识产权商品
    @Override
    public Object SelectONIntellectualProperty(PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<IntellectualProperty> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        QueryWrapper<IntellectualProperty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",OnOrOff.ON);
        Page<IntellectualProperty> resultPage = intellectualPropertyMapper.selectPage(page,queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        resultMap.put("result",result);
        return resultMap;
    }

    /**
     * end-lzh
     */

    /**
     * start-hwf
     */
    /**
     * end-hwf
     */

    /**
     * start-hqq
     */
    //按名称查看已上架知识产权
    @Override
    public Object getIntellectualPropertyOnByName(PageRequestInfo pageRequestInfo,String name){
        QueryWrapper<IntellectualProperty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",OnOrOff.ON);
        if(name!=null){
            queryWrapper.like("name",name);
        }
        Page<IntellectualProperty> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<IntellectualProperty> resultPage = intellectualPropertyMapper.selectPage(page,queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        resultMap.put("intellectualPropertyList",result);
        return resultMap;
    }
    /**
     * end-hqq
     */
}
